// pages/expressbox/expressadd/expressadd.js
import {
  getexpressinit,
  saveExpress,
  delExpress
} from '../../../utils/networkRequest.js'
import {
  errorMsg
} from '../../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    expresslist: [],
    expindex: "",
    dzmdlist: [],
    dzmdindex: "",

    mothAccount: "",
    mianDanAccount: "",
    mianDanPwd: "",
    daiShouAccount: "",
    isEdit:false,
    id:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    if (options.item){
      let item = JSON.parse(options.item);
      getexpressinit((r) => {
        if (r.code == 0) {
          self.setData({
            expresslist: r.data,
            dzmdlist: r.data[item.com_id - 1].template,
            isEdit: options.isEdit ? options.isEdit : false,
            expindex:item.com_id-1, 
            dzmdindex: item.tmpl_id-1,
            mothAccount: item.month_code,
            mianDanAccount: item.customer_name,
            mianDanPwd: item.customer_pwd,
            daiShouAccount: item.cod,
            id:item.id

          })
        } else {
          errorMsg(r.message);
        }
      })

    }else{
      getexpressinit((r) => {
        if (r.code == 0) {
          self.setData({
            expresslist: r.data, 
          })
        } else {
          errorMsg(r.message);
        }
      })
    }
   
    
  },
  //选择快递公司
  bindPickerChange1: function(e) {
    this.setData({
      expindex: e.detail.value,
      dzmdlist: this.data.expresslist[e.detail.value].template,
      dzmdindex: ""
    })
  },
  //选择电子面单
  bindPickerChange2: function(e) {
    this.setData({
      dzmdindex: e.detail.value,

    })
  },
  //获取月结账号
  getMothAccount: function(e) {
    this.setData({
      mothAccount: e.detail.value
    })
  },
  //获取面单账号
  getMianDanAccount: function(e) {
    this.setData({
      mianDanAccount: e.detail.value
    })
  },
  //获取面单密码
  getMianDanPwd: function(e) {
    this.setData({
      mianDanPwd: e.detail.value
    })
  },
  //获取代收货款账号
  getDaiShouAccount: function(e) {
    this.setData({
      daiShouAccount: e.detail.value
    })
  },
  onDel: function (e) {
    let self = this;
    let id =  this.data.id

    wx.showModal({
      title: '删除快递公司',
      content: '确定要删除该快递公司吗？',
      showCancel: true, //是否显示取消按钮 
      confirmColor: '#ff6a6a', //确定文字的颜色
      success: function (res) {
        if (!res.cancel) {
          delExpress(id, (r) => {
            if (r.code == 0) {

              self.setData({
                dataList: r.data
              })
              wx.navigateBack()
              setTimeout(function(){
                errorMsg(r.message);
              },400)
              
            } else {
              errorMsg(r.message);
            }
          })
        }
      },
      fail: function (res) { }, //接口调用失败的回调函数
      complete: function (res) { }, //接口调用结束的回调函数（调用成功、失败都会执行）
    })
  },
  //提交快递公司信息
  onSubmit: function() {
    let {
      expresslist,
      expindex,
      dzmdlist,
      dzmdindex,
      mothAccount,
      mianDanAccount,
      mianDanPwd,
      daiShouAccount,
      id
    } = this.data;
    if (expindex === "") {
      errorMsg("请选择快递公司");
    } else if (dzmdindex === "") {
      errorMsg("请选择电子面单模版")
    } else {
      saveExpress(id,expresslist[expindex].code, dzmdlist[dzmdindex].id, mothAccount, mianDanAccount, mianDanPwd, daiShouAccount, (r) => {
        if (r.code == 0) {
          wx.navigateBack()
          setTimeout(function() {
            errorMsg(r.message);
          }, 400)
        } else {
          errorMsg(r.message);
        }

      })

    }


  }
})